# Figure_A2.R
# John G. Bullock (john@johnbullock.org)
# 2016 August 02

# This file creates Figure A2 in 
#   Gertler, Aaron L., and John G. Bullock.  2016.  "Reference Rot: An 
#   Emerging Threat to Transparency in Political Science."  PS: Political 
#   Science and Politics.
#
# Figure A2 has two side-by-side panels.  The left-hand panel plots, for each 
# year (2000-2013), the mean number of URLs per APSR issue.  The right-hand 
# panel plots the mean number of URLs per APSR article (not issue) over the 
# same period.


library(grid)
library(lattice)
source('URL_dataCoding.R')



##############################################################################
# PRELIMINARIES FOR PRINTING THE FIGURE
##############################################################################
filenameStem     <- 'Figure_A2'
PDFtitle         <- 'Mean Number of URLs in Each Issue and Each Article of the APSR, By Year, 2000-2013'
PS_width         <- 9.5
PS_height        <- 12.0
postscriptBackground <- 'transparent'
panelHeight      <- list(2.75, "inches")  
panelWidth       <- list(2.50, "inches")
panelLayout      <- c(2, 1)  # columns, then rows  
xBetween         <- 3.5 
plotLineColor    <- c('black', 'gray')
plotLineWidth    <- 2
panelBorderCol   <- 'black'
panelBorderWidth <- .3
baseCexSize      <- 1.0
xLabSize         <-  .775 * baseCexSize
yLabSize         <-  .775 * baseCexSize
axisTickSize     <- .4
xAxisLabels      <- seq(2000, 2012, by = 2)
xAxisLabels      <- strsplit(paste0(xAxisLabels, collapse = '  '), ' ')[[1]] 
xAxis            <- list(
  draw   = TRUE, 
  labels = c(xAxisLabels, ''), 
  at     = 2000:2013,
  tck    = c(axisTickSize, 0), 
  col    = panelBorderCol, 
  cex    = xLabSize,
  alternating = 1,
  axs    = 'i')  # axs = 'i' means that there is no padding around the xlimits
yAxisLabels.left      <- seq(10, 40, by = 10)
yAxisLabels.left      <- strsplit(paste0(yAxisLabels.left, collapse = '  '), ' ')[[1]]
yAxisLabels.right     <- 1:4
yAxisLabels.right     <- strsplit(paste0(yAxisLabels.right, collapse = '  '), ' ')[[1]]
yAxis            <- list(
  draw   = TRUE,
  relation = 'free',
  alternating = 1,
  limits = list(c(0, 50), c(0,5)),
  labels = list(
    c('', yAxisLabels.left, ''),
    c('', yAxisLabels.right, '')),
  at     = list(
    seq(5, 45, by = 5),
    seq(0.5, 4.5, by = .5)),
  tck    = c(axisTickSize, 0),
  col    = panelBorderCol,
  cex    = yLabSize,
  rot    = 90,
  axs    = 'i')



##############################################################################
# CREATE THE DATA FRAME 
##############################################################################
# After creating the data frame, change the order of levels in the "type" 
# variable so that the per-issue results are plotted in the left-hand panel,
# the per-article results in the right-hand panel.  [2015 07 27]
URLsByYear_data <- data.frame(
  year           = rep(2000:2013, 2),
  means = c(with(URL_data, tapply(URL, year, length))/4,
            with(URL_data, tapply(URL, year, length))/articlesPerYear),
  type = rep(qw("perIssue perArticle"), each = 14))
URLsByYear_data$type <- relevel(URLsByYear_data$type, "perIssue")



##############################################################################
# PANEL FUNCTION
##############################################################################
myPanel <- function (...) {
  panel.xyplot(...)
  
  # TICK MARKS AT TOP AND RIGHT
  panel.axis(
    side        = "top",
    at          = xAxis$at,
    draw.labels = FALSE,
    half        = FALSE,
    tck         = c(2*axisTickSize, axisTickSize))
  panel.axis(
    side        = "right",
    at          = yAxis$at[[panel.number()]],
    draw.labels = FALSE,
    half        = FALSE,
    outside     = FALSE,
    tck         = c(axisTickSize, 2*axisTickSize))
  
  # Y-AXIS LABELS
  yAxisLabels <- c('Mean URLs Per Issue', 'Mean URLs Per Article')
  grid.text(
    label = yAxisLabels[[panel.number()]],
    x     = -.14,
    y     =  .50,
    rot   = 90,
    gp    = gpar(cex = .95 * baseCexSize))
  
}  
  

  
##############################################################################
# DRAW THE FIGURE
##############################################################################
setwd('figureOutput/')
pdf(
  file   = paste0(filenameStem, '.pdf'), 
  width  = PS_width, 
  height = PS_height, 
  paper  = "special", 
  title  = PDFtitle,
  bg     = postscriptBackground)
trellis.par.set(
  axis.components = list(
    left  = list(
      pad1 = 0.35,    # distance between axis ticks and tick labels
      pad2 = 1.00)),  # distance between axis labels and big y-axis label
  axis.line = list(
    alpha = 1, 
    col   = panelBorderCol,    
    lty   = 1, 
    lwd   = panelBorderWidth),  
  plot.line = list(
    col = 'blue'),
  superpose.line = list(
    alpha = c(1,1), 
    col   = c('black', plotLineColor), 
    lty   = c("43", "solid"), 
    lwd   = c(1, plotLineWidth)))
URLsPerIssuePlot <- xyplot(
  means ~ year | type,
  data       = URLsByYear_data, 
  type       = 'l',  
  strip      = FALSE,
  panel      = myPanel,
  layout     = panelLayout,
  between    = list(x = xBetween, y = 0),
  scales     = list(x = xAxis, y = yAxis),
  ylab       = '',
  xlab       = '',
  par.settings = trellis.par.set("clip", list(panel="off", strip="off"))
)
print(
  x            = URLsPerIssuePlot, 
  panel.width  = panelWidth, 
  panel.height = panelHeight)
dev.off()
